// Copyright (c) [2021] [daifang]
// [Dcomponent] is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
import './index.less'
import {useEffect, useState} from 'react'
import ReactDom  from 'react-dom'
import { object, string } from 'prop-types'

function Content(props){
    let {type, option, children} = props
    let [animateName, setAnimateName] = useState('fadeIn')
    useEffect(() => {
        let timer = setTimeout(() => {
            setAnimateName('fadeOut')
            let timer = setTimeout(()=>{
                let div = document.getElementsByClassName('toast-container')[0];
                ReactDom.render(null, div)
                // 隐藏之后的回调
                if(option.onHide)
                    option.onHide()
                clearTimeout(timer)
            },300)
        }, option.delay || 2000);
        return () => {
            clearTimeout(timer)
        }
    })
    return (
        <div 
            className={`toast-item ${option.position&&''} ${type} animated faster ${animateName}`}
        >
            {<i className={`iconfont icon-${type}`}></i>}
            <div className='content'>{children}</div>
        </div>
    )
}

Content.propsType = {
    option:object,
    type:string,
    position:string
}
Content.defaultProps = {
    option:{
        delay:1000,
        icon:true,
        position:'',
        onHide:()=>{}
    }
}
export default Content